package com.yulan.bankback.service.bank;


import com.yulan.bankback.converter.UserConverter;
import com.yulan.bankback.entity.constant.RedisKeyConstant;
import com.yulan.bankback.entity.constant.RiskConstant;
import com.yulan.bankback.entity.event.AttackEvent;
import com.yulan.bankback.entity.model.admin.AbnormalUser;
import com.yulan.bankback.entity.model.admin.webAttack.IpBlackList;
import com.yulan.bankback.entity.model.bank.BankUser;
import com.yulan.bankback.log.Logger;
import com.yulan.bankback.mapper.admin.AbnormalUserMapper;
import com.yulan.bankback.mapper.admin.webAttack.AttackEventMapper;
import com.yulan.bankback.mapper.admin.webAttack.IpBlackListMapper;
import com.yulan.bankback.mapper.bank.BankUserMapper;
import com.yulan.bankback.util.IpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


@Service
public class RiskService {

    @Resource
    private BankUserMapper bankUserMapper;

    @Resource
    private AbnormalUserMapper abnormalUserMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private IpBlackListMapper ipBlackListMapper;

    @Autowired
    private UserConverter userConverter;

    @Autowired
    private AttackEventMapper attackEventMapper;

    @Autowired
    private Logger logger;


    public void checkRemoteChangePwd(String uid, String ip){
        BankUser user = bankUserMapper.selectById(uid);
        if(!checkRemoteLogin(uid)){
            return;
        }

        logger.logAbnormal(uid,"异地修改密码");
        AbnormalUser abnormalUser = userConverter.convertUserToAbnormal(user, RiskConstant.REMOTE_CHANGE_PWD, ip);
        abnormalUserMapper.insert(abnormalUser);
    }


    public void checkRemoteChangePhone(String uid, String ip){
        BankUser user = bankUserMapper.selectById(uid);
        if(!checkRemoteLogin(uid)){
            return;
        }

        logger.logAbnormal(uid,"异地修改手机号");
        AbnormalUser abnormalUser = userConverter.convertUserToAbnormal(user, RiskConstant.REMOTE_CHANGE_PHONE, ip);
        abnormalUserMapper.insert(abnormalUser);
    }

    public void checkRemoteChangePayPwd(String uid, String ip){
        BankUser user = bankUserMapper.selectById(uid);
        if(!checkRemoteLogin(uid)){
            return;
        }

        logger.logAbnormal(uid,"异地修改支付密码");
        AbnormalUser abnormalUser = userConverter.convertUserToAbnormal(user, RiskConstant.REMOTE_CHANGE_PAYPWD, ip);
        abnormalUserMapper.insert(abnormalUser);
    }

    public void checkRemoteBindCard(String uid, String ip){
        BankUser user = bankUserMapper.selectById(uid);
        if(!checkRemoteLogin(uid)){
            return;
        }

        logger.logAbnormal(uid,"异地绑定银行卡");
        AbnormalUser abnormalUser = userConverter.convertUserToAbnormal(user, RiskConstant.REMOTE_BIND_CARD, ip);
        abnormalUserMapper.insert(abnormalUser);
    }

    public void checkRemoteWithdrawal(String uid, String ip){
        BankUser user = bankUserMapper.selectById(uid);
        if(!checkRemoteLogin(uid)){
            return;
        }

        logger.logAbnormal(uid,"异地提现");
        AbnormalUser abnormalUser = userConverter.convertUserToAbnormal(user, RiskConstant.REMOTE_WITHDRAWAL, ip);
        abnormalUserMapper.insert(abnormalUser);
    }

    public void checkRemoteTransfer(String uid, String ip){
        BankUser user = bankUserMapper.selectById(uid);
        if(!checkRemoteLogin(uid)){
            return;
        }

        logger.logAbnormal(uid,"异地转账");
        AbnormalUser abnormalUser = userConverter.convertUserToAbnormal(user, RiskConstant.REMOTE_TRANSFER, ip);
        abnormalUserMapper.insert(abnormalUser);
    }


    public void addAttackEvent(String ip, String type, Integer attackLevel, String city){
        AttackEvent attackEvent = new AttackEvent();
        attackEvent.setIp(ip);
        attackEvent.setType(type);
        attackEvent.setCreateTime(new Date());
        attackEvent.setAttackLevel(attackLevel);
        attackEvent.setIsBlack(0);
        attackEventMapper.insert(attackEvent);

        IpBlackList ipBlackList = new IpBlackList();
        ipBlackList.setIp(ip);
        ipBlackList.setCity(city);
        ipBlackList.setAttackType(type);
        ipBlackList.setCreateTime(new Date());
        ipBlackListMapper.insert(ipBlackList);
    }

    private boolean checkRemoteLogin(String uid){
        String hasRemoteLogin = redisService.hGetByKey(RedisKeyConstant.REMOTE_LOGIN_MAP, uid);
        return hasRemoteLogin != null && StringUtils.equals(hasRemoteLogin, "1");
    }



}
